﻿Imports System.Text.RegularExpressions


Public Class Valider

    Public Shared Function sjekkEpost(ByVal epost As String) As Boolean
        Dim pattern As String = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
        Dim sjekk As Match = Regex.Match(epost, pattern)
        If sjekk.Success Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Shared Function sjekkNavnefelt(ByVal tekst As String) As Boolean

        Dim pattern As String = "^[a-æøåA-ÆØÅ ]*$"
        Dim sjekk As Match = Regex.Match(tekst, pattern)
        If sjekk.Success Then
            Return sjekkForInnhold(tekst)
        Else
            Return False
        End If

    End Function

    Public Shared Function sjekkAdressefelt(ByVal tekst As String) As Boolean
        Dim pattern As String = "^[a-æøåA-ÆØÅ0-9., ]*$"
        Dim sjekk As Match = Regex.Match(tekst, pattern)
        If sjekk.Success Then
            Return sjekkForInnhold(tekst)
        Else
            Return False
        End If
    End Function

    Public Shared Function sjekkBruker(ByVal tekst As String) As Boolean
        Dim MIN As Integer = 3
        Dim minLengde As Boolean = sjekkLengde(tekst, MIN)
        Dim inneholderTall As Boolean = sjekkOmTall(tekst)

        Return minLengde And inneholderTall

    End Function

    Public Shared Function sjekkTelefonfelt(ByVal tlfNr As String) As Boolean
        Dim nr As Integer
        Try
            nr = CInt(tlfNr)
            If nr >= 10000000 And nr < 99999999 Then
                Return True
            End If
        Catch ex As Exception
            Return False
        End Try

        Return False

    End Function

    Public Shared Function sjekkForInnhold(ByVal feltinnhold As String) As Boolean
        Dim lengde As Integer = feltinnhold.Count()
        If lengde > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Shared Function sjekkLengde(ByVal tekst As String, ByVal min As Integer) As Boolean
        If tekst.Length >= min Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Shared Function sjekkOmTall(tekst As String) As Boolean
        Dim bokstaver() As Char = tekst.ToCharArray
        For Each tegn In bokstaver
            If IsNumeric(tegn) Then
                Return True
                Exit For
            End If
        Next
        Return False
    End Function

End Class